


<!--
All rights Reserved, Designed By www.youyacao.com
@Description:VIP列表
@author:成都市一颗优雅草科技有限公司
@version V4.2
注意：本前端源码遵循 MIT开源协议本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:422108995 23625059584
开源gitee地址：https://gitee.com/youyacao/songshu-video-page
 -->

<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> VIP商品列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
        >批量删除
        </el-button>
        <!-- <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select>-->
        <!--<el-input v-model="query.name" placeholder="用户名/用户ID/手机号" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>-->
        <el-button
          style="float: right;margin-bottom:20px; "
          type="primary"
          icon="el-icon-lx-add"
          class="handle-del mr10"
          @click="addData"
        >添加
        </el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="title" label="商品名"></el-table-column>

        <el-table-column label="描述">
          <template slot-scope="scope">{{scope.row.desc==null?'未设置':scope.row.desc}}</template>
        </el-table-column>
        <el-table-column prop="price" label="金额"></el-table-column>
        <el-table-column label="价值时间">
          <template slot-scope="scope">{{scope.row.time!=null?scope.row.time+'天':"0"}}</template>
        </el-table-column>

        <el-table-column prop="create_time" label="创建时间"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tooltip class="item pointer" effect="dark" content="点击修改状态" placement="right">
              <el-tag v-if="scope.row.state==0" type="success" @click="handleState(scope.$index, scope.row)">启用</el-tag>
              <el-tag v-if="scope.row.state==1" type="danger" @click="handleState(scope.$index, scope.row)">禁用</el-tag>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.page"
          :page-size="query.pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="商品名">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
        <el-form-item label="金额">
          <el-input type="text" oninput="value=value.replace(/[^0-9.]/g,'')" v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="价值时间(天)" prop="vip_end">
          <el-input type="number" v-model="form.time"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加弹出框 -->
    <el-dialog title="添加" :visible.sync="addVisible" width="30%">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="商品名">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
        <el-form-item label="金额">
          <el-input type="text" oninput="value=value.replace(/[^0-9.]/g,'')" v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="价值时间(天)" prop="vip_end">
          <el-input type="number" v-model="form.time"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDataSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'basetable',
    data() {
      return {
        fileList: [],
        query: {
          name: '',
          page: 1,
          pageSize: 10
        },
        tableData: [],
        multipleSelection: [],
        delList: [],
        editVisible: false,
        addVisible: false,
        total: 0,
        form: {},
        idx: -1,
        id: -1
      }
    },
    created() {
      this.getData()
    },
    methods: {
      // 获取 easy-mock 的模拟数据
      getData() {
        this.$api.vipshop.list(this.query).then(res => {
          this.tableData = res.data
          this.total = res.total || 0
        })
      },
      // 触发搜索按钮
      handleSearch() {
        this.$set(this.query, 'page', 1)
        this.getData()
      },
      handleState(index, row) {
        console.log(row);
        let temp_row = row
        temp_row.state = temp_row.state == 1 ? 0 : 1;
        this.$api.vipshop.update(temp_row).then(res => {
          if (res.code == 1) {
            this.$message.error(`修改失败,` + res.msg)
            return
          }
          this.editVisible = false
          this.$message.success(`修改成功`)
          this.getData()
        })
      },
      // 删除操作
      handleDelete(index, row) {
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        })
          .then(() => {

            var arr = [row.id]
            this.$api.vipshop.delete({ids: arr}).then(res => {
              if (res.code == 1) {
                this.$message.error('删除失败,' + res.msg)
                return
              }
              this.getData()
              this.$message.success('删除成功')
            })
          })
          .catch(() => {
          })
      },
      // 多选操作
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      delAllSelection() {
        const length = this.multipleSelection.length
        var arr = []
        this.delList = this.delList.concat(this.multipleSelection)
        for (let i = 0; i < length; i++) {
          arr.push(this.multipleSelection[i].id)
        }
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        })
          .then(() => {
            this.$api.vipshop.delete({ids: arr}).then(res => {
              if (res.code == 1) {
                this.$message.error('删除失败,' + res.msg)
                return
              }
              this.getData()
              this.$message.success('删除成功')
            })
          })
          .catch(() => {
          })
        this.multipleSelection = []
      },
      // 编辑操作
      handleEdit(index, row) {
        this.idx = index
        this.form = row
        this.editVisible = true
      },
      // 保存编辑
      saveEdit() {
        this.$api.vipshop.update(this.form).then((res) => {
          if (res.code == 1) {
            this.$message.error(`修改失败,` + res.msg)
            return
          }
          this.editVisible = false
          this.$message.success(`修改成功`)
          this.$set(this.tableData, this.idx, this.form)
        })

      },
      // 分页导航
      handlePageChange(val) {
        this.$set(this.query, 'page', val)
        this.getData()
      },
      uploadSuccess(response, file, fileList) {
        if (response.code === 0) {
          this.form.head_img = response.data.url
          this.fileList = []
        } else {
          this.$message.error('上传失败,' + response.msg)
        }

      },
      addData() {
        this.form = {};
        this.addVisible = true;
      },
      addDataSubmit() {
        this.$api.vipshop.add(this.form).then(res => {
          if (res.code == 1) {
            this.$message.error(res.msg)
            return
          }
          this.addVisible = false
          this.$message.success(`添加成功`)
          this.getData()
        })
      },
    }
  }
</script>

<style scoped>
  .pointer {
    cursor: pointer;
  }

  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .table {
    width: 100%;
    font-size: 14px;
  }

  .red {
    color: #ff0000;
  }

  .mr10 {
    margin-right: 10px;
  }

  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }

  .type-icon {
    display: block;
    width: 150px;
    height: 150px;
  }
</style>
